package com.nutstudy.mybatis.test;

import com.nutstudy.mybatis.org.mybatis.spring.NutImportBeanDefinitionRegister;
import com.nutstudy.mybatis.org.mybatis.spring.NutMapperScan;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.io.InputStream;


/**
 * @author: HyoJoo-W
 * @date: 2021-08-28 13:47
 * @description:
 */

@NutMapperScan("com.nutstudy.mybatis.test")
@ComponentScan("com.nutstudy.mybatis.test")
public class Main
{
    public static void main(String[] args)
    {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(Main.class);

        context.refresh();

        UserService userService = context.getBean("userService", UserService.class);
        userService.test();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws IOException
    {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }
}
